package com.wfh.mianshiji.ai.rag;

import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.filter.Filter;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;

/**
 * @Title: RagCustomAdvisorFactory
 * @Author wangfenghuan
 * @Package com.wfh.mianshiji.ai.rag
 * @Date 2025/11/3 14:21
 * @description: 自定义检索增前服务
 */
public class RagCustomAdvisorFactory {

    /**
     * 创建自定义向量存储advisor
     * @param vectorStore
     * @param status
     * @return
     */
    public static Advisor createRagAdvisor(VectorStore vectorStore, String status){
        Filter.Expression expression = new FilterExpressionBuilder()
                .eq("status", status)
                .build();
        VectorStoreDocumentRetriever retriever = VectorStoreDocumentRetriever.builder()
                .vectorStore(vectorStore)
                // 过滤条件
                .filterExpression(expression)
                // 相似度阈值
                .similarityThreshold(0.7)
                // 返回文档数量
                .topK(10)
                .build();
        return RetrievalAugmentationAdvisor.builder()
                .documentRetriever(retriever)
                .build();
    }

}
